﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PDX;
using PDX.Services;

namespace PdxDemo.FluentValidationDemo
{
    public class FluentValidationAdapter : PDX.Validation.IValidationAdapter
    {
        public string GetMessage(Context context)
        {
            var resolution = context.GetResolution();
            if (resolution == null)
            {
                return string.Empty;
            }
            
            var fluentValidated = resolution.ResolvedDC as FluentValidationDemo.IHaveValidator;
            if (fluentValidated != null)
            {
                var validator = fluentValidated.GetValidator();
                var result = validator.Validate(resolution.ResolvedDC);

                foreach (var error in result.Errors)
                {
                    if (error.PropertyName == resolution.Property.Name)
                    {
                        return error.ErrorMessage;
                    }
                }
            }

            return string.Empty;
        }
    }
}
